package com.youhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.mapper.SchoolMapper;
import com.youhome.model.School;
import com.youhome.service.SchoolService;
import com.youhome.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by chen365 on 2018/05/17.
 */
@Service
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    private SchoolMapper schoolMapper;

    @Override
    public PageInfo<School> selectByBean(Integer pageIndex, Integer pageSize, School record) {
        if (StringUtils.isEmpty(pageIndex) || pageIndex < 1) {
            pageIndex = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 10) {
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<School> schools = schoolMapper.selectByBean(record);

        return new PageInfo<>(schools);
    }

    @Override
    public List<School> selectAllNotUnfiled(Integer pageIndex, Integer pageSize) {
        if (StringUtils.isEmpty(pageIndex) || pageIndex < 1) {
            pageIndex = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 10) {
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<School> schools = schoolMapper.selectAllNotUnfiled();
        return schools;
    }

    @Override
    public void analysisGrade() {

        ArrayList<String> grade_all = new ArrayList();
        grade_all.add(0, "PK");
        grade_all.add(1, "K");

        grade_all.add(2, "1");
        grade_all.add(3, "2");
        grade_all.add(4, "3");
        grade_all.add(5, "4");
        grade_all.add(6, "5");

        grade_all.add(7, "6");
        grade_all.add(8, "7");
        grade_all.add(9, "8");

        grade_all.add(10, "9");
        grade_all.add(11, "10");
        grade_all.add(12, "11");
        grade_all.add(13, "12");

        grade_all.add(14, "UNGRADED");
        grade_all.add(15, "N/A");

        ArrayList<String> e_school = new ArrayList(); //小学
        e_school.add(0, "1");
        e_school.add(1, "2");
        e_school.add(2, "3");
        e_school.add(3, "4");
        e_school.add(4, "5");

        ArrayList<String> m_school = new ArrayList(); //初中
        m_school.add(0, "6");
        m_school.add(1, "7");
        m_school.add(2, "8");

        ArrayList<String> h_school = new ArrayList(); //高中
        h_school.add(0, "9");
        h_school.add(1, "10");
        h_school.add(2, "11");
        h_school.add(3, "12");

        String[] grades = new String[]{"PK", "K", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "UNGRADED", "N/A"};


        Integer pageSize = 1000;
        for (int pageIndex = 1; pageIndex <= 219; pageIndex++) {
            List<School> list = selectAllNotUnfiled(pageIndex, pageSize);
            if (list.size() > 0) {
                for (School school : list) {

                    School entity = new School();
                    if (school.getGrade() != null) {
                        String[] items = school.getGrade().split(",|&");

                        boolean has_e = false;
                        boolean has_m = false;
                        boolean has_h = false;

                        for (String item : items) {
                            item = item.trim().toUpperCase();

                            if (item.indexOf("-") > -1) {
                                String a = item.split("-")[0];
                                String b = item.split("-")[1];

                                int begin_position = StringUtil.getPosition(grades, a);
                                int end_position = StringUtil.getPosition(grades, b);
                                String[] arr = Arrays.copyOfRange(grades, begin_position, end_position + 1);

                                final ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(arr));

                                if (!has_e) {
                                    ArrayList<String> pj_e = (ArrayList<String>) arrayList.clone();
                                    pj_e.retainAll(e_school);
                                    if (pj_e.size() > 0) has_e = true;
                                }
                                if (!has_m) {
                                    ArrayList<String> pj_m = (ArrayList<String>) arrayList.clone();
                                    pj_m.retainAll(m_school);
                                    if (pj_m.size() > 0) has_m = true;
                                }

                                if (!has_h) {
                                    ArrayList<String> pj_h = (ArrayList<String>) arrayList.clone();
                                    pj_h.retainAll(h_school);
                                    if (pj_h.size() > 0) has_h = true;
                                }

                            } else {
                                final ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(item));
                                if (!has_e) {
                                    ArrayList<String> pj_e = (ArrayList<String>) arrayList.clone();
                                    pj_e.retainAll(e_school);
                                    if (pj_e.size() > 0) has_e = true;
                                }
                                if (!has_m) {
                                    ArrayList<String> pj_m = (ArrayList<String>) arrayList.clone();
                                    pj_m.retainAll(m_school);
                                    if (pj_m.size() > 0) has_m = true;
                                }

                                if (!has_h) {
                                    ArrayList<String> pj_h = (ArrayList<String>) arrayList.clone();
                                    pj_h.retainAll(h_school);
                                    if (pj_h.size() > 0) has_h = true;
                                }
                            }
                        }

                        entity.setId(school.getId());
                        entity.setHasElementary(has_e);
                        entity.setHasMiddle(has_m);
                        entity.setHasHigh(has_h);
                        schoolMapper.updateByPrimaryKeySelective(entity);
                    }

                }
            }
        }
    }
}
